package com.dz.lm.service.attach.impl;

import com.dz.lm.common.dto.AttachDTO;
import com.dz.lm.common.dto.PageDto;
import com.dz.lm.common.exception.DaoException;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.dao.attach.read.AttachDao;
import com.dz.lm.domain.attach.Attach;
import com.dz.lm.domain.attach.AttachExample;
import com.dz.lm.service.attach.AttachService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.source.util.DocTreeScanner;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AttachServiceImpl implements AttachService {
    @Resource( name = "readAttachDao")
    private AttachDao readAttachDao;
    @Resource( name = "writeAttachDao")
    private com.dz.lm.dao.attach.write.AttachDao writeAttachDao;
    @Transactional
    @Override
    public int add(Attach attach) throws ServiceException {
        int i =0;
        try {
            i = writeAttachDao.insertSelective(attach);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Transactional
    @Override
    public int delete(long attachId) throws ServiceException {
        int i =0;
        try {
            i = writeAttachDao.deleteByPrimaryKey(attachId);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public AttachDTO sleletById(long attachId) throws ServiceException {
        Attach attach =null;
        AttachDTO dto = new AttachDTO();
        try {
            attach = readAttachDao.selectByPrimaryKey(attachId);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        if(attach != null){
            BeanUtils.copyProperties(attach,dto);
        }
        return dto;
    }

    @Override
    public PageDto<AttachDTO> query(int page,int rwos,int type, int attachType, int fileId, String name, long userId) throws ServiceException {
        PageDto<AttachDTO> pageDto = new PageDto();
        AttachExample example = new AttachExample();
        //使用插件  页码加1
        PageHelper.startPage(page+1,rwos);
        List<AttachDTO> dtos = new ArrayList();
        AttachExample.Criteria criteria = example.createCriteria();
        if(userId >0){
            criteria.andUserIdEqualTo(userId);
        }
       if(type >0){
           criteria.andTypeEqualTo(type);
       }

        List<Attach> attaches = null;
        try {
            attaches = readAttachDao.selectByExample(example);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        if(attaches != null && attaches.size()>0){
            PageInfo pageInfo = new PageInfo(attaches);
            for (Attach attach : attaches) {
                AttachDTO dto = new AttachDTO();
                BeanUtils.copyProperties(attach,dto);
                dtos.add(dto);
            }
            pageDto.setPage(pageInfo.getPages());
            pageDto.setTotal(pageInfo.getTotal());
            pageDto.setList(dtos);

        }
        return pageDto;
    }

    @Override
    public PageDto<AttachDTO> queryByUserId(int page,int rwos,long userId) throws ServiceException {
        PageDto<AttachDTO> pageDto = new PageDto();
        AttachExample example = new AttachExample();
        List<AttachDTO> dtos = new ArrayList();
        AttachExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        //使用插件  页码加1
        PageHelper.startPage(page+1,rwos);
        List<Attach> attaches = null;
        try {
            attaches = readAttachDao.selectByExample(example);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        if(attaches != null && attaches.size()>0){
            PageInfo pageInfo = new PageInfo(attaches);
            for (Attach attach : attaches) {
                AttachDTO dto = new AttachDTO();
                BeanUtils.copyProperties(attach,dto);
                dtos.add(dto);
            }
            pageDto.setPage(pageInfo.getPages());
            pageDto.setTotal(pageInfo.getTotal());
            pageDto.setList(dtos);

        }
        return pageDto;
    }
}
